/***************************************************************
Made by: Cultured::Labs

Copyright (c) 2010 Cultured::Labs
****************************************************************/
#include <iostream>
#include <algorithm>
#include "decipherment.h"
#include "transform.h"
using namespace std;

//En(x) = (x + n) mod 26
string encryption(string plaintext, int shift) {
  int plain_number;
  int cipher_number;
  char cipher;
  string temp;
  string ciphertext;

  transform(plaintext.begin(), plaintext.end(), plaintext.begin(), ::tolower);

  for (unsigned int index = 0; index < plaintext.size(); index++) {
    plain_number = characterToNumber(plaintext[index]);
    cipher_number = (plain_number + shift) % 26;
    cipher = numberToCharacter(cipher_number);
    temp = cipher;
    ciphertext.append(temp);
  }

  transform(ciphertext.begin(), ciphertext.end(), ciphertext.begin(), ::toupper);
  return ciphertext;
}


//Dn(x) = (x - n + 26) mod 26
string decryption(string ciphertext, int shift) {
  int cipher_number;
  int plain_number;
  char plain;
  string temp;
  string plaintext;

  transform(ciphertext.begin(), ciphertext.end(), ciphertext.begin(), ::tolower);

  for (unsigned int index = 0; index < ciphertext.size(); index++) {
    cipher_number = characterToNumber(ciphertext[index]);
    plain_number = (cipher_number - shift + 26) % 26;
    plain = numberToCharacter(plain_number);
    temp = plain;
    plaintext.append(temp);
  }

  return plaintext;
}
